<?php
namespace Common\Model;
use Think\Model;
class EntCustModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_ent_cust'; 
	
	public function getCustByDepart($departid){
		$map['departid'] = $departid;
		$fields = array('custid','departid','entid','position','nickname');
		$result = $this->field($fields)->where($map)->select();
		if(empty($result)) $result = array();
		return $result;
	}
	
	public function getCustByEnt($entid){
		$map['entid'] = $entid;
		$fields = array('custid','departid','entid','position','nickname');
		$result = $this->field($fields)->where($map)->select();
		if(empty($result)) $result = array();
		return $result;
	}
	
	
	public function getCustInfo($entid,$custid){
		$map['custid'] = $custid;
		$map['entid'] = $entid;
		$fields = array('custid','departid','entid','position','nickname');
		$result = $this->field($fields)->where($map)->select();
		if(empty($result)) $result = array();
		return $result;
	}
	
	
	public function addCust($data){
		$map['custid'] = $data['custid'];
		$map['departid'] = $data['departid'];
		$result = $this->where($map)->find();
		if(!empty($result)){
			return $result['id'];
		}else{
			$result = $this->add($data);
			return $result;
		}
	}
	
	
	public function deleteCust($data){
		if(!empty($data['departid'])) $map['departid'] = $data['departid'];
		if(!empty($data['entid'])) $map['entid'] = $data['entid'];
		if(!empty($data['custid'])) $map['custid'] = $data['custid'];
		if(!empty($data['custids'])) $map['custid'] = array('in' , $data['custids']);
		$result = $this->where($map)->delete();
		return $result;
	}
	
	
	public function updateCust($data){
		$map['custid'] = $data['custid'];
		$map['departid'] = $data['departid'];
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	
	public function updateCustById($data){
		$map['custid'] = $data['custid'];
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	
	public function exitEnt($entid , $custid){
		$map['entid'] = $entid;
		$map['custid'] = $custid;
		$result = $this->where($map)->delete();
		return $result;
	}
	
}


